#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h> /* Inclusion du header de SDL_image (adapter le dossier au besoin) */
Uint8 r=10,g=10,b=10,r1,g1,b1,r2,g2,b2,r3,g3,b3,r4,g4,b4;
void sawer (SDL_Surface* img,int x ,int y ,SDL_Surface* ecr);
void getPixelColor(int x, int y, SDL_Surface *map,Uint8 *r,Uint8 *g,Uint8 *b);
void paint();
void paintflous();
void paintstop();
void paintstopflous();
void coli ();

int verif=0 ;
int x;
int nagz;
int i=0 ;
int x1=0 ;
int c=1;
int action1=0,test,testf=0;
int action2=0;
int continuer = 1, jump=0;
int indice = 0;
int keyR = 0, keyUP = 0;
int ppx=0,ppy=150,flousx=250,flousy=160,fondx=0,fondy=0;
SDL_Event event;
SDL_Surface *ecran = NULL,
*back = NULL,*back1=NULL, *perso[11],*flous[5]; 


int main()
{





SDL_Init(SDL_INIT_VIDEO);


   // SDL_WM_SetIcon(IMG_Load("sdl_icone.bmp"), NULL);

    ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE |SDL_DOUBLEBUF);
    SDL_WM_SetCaption("Mohamed Mehmli LVL", NULL);
 SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
     back= IMG_Load("monjeu/hakim.jpg");

   back1= IMG_Load("monjeu/ert1.jpg");





/**/
flous[0] = IMG_Load("flous/1.png");
flous[1] = IMG_Load("flous/2.png");
flous[2] = IMG_Load("flous/3.png");
flous[3] = IMG_Load("flous/4.png");
flous[4] = IMG_Load("flous/5.png");
flous[5] = IMG_Load("flous/6.png");

/**/




  perso[0] = IMG_Load("monjeu/1.png");
 perso[1] = IMG_Load("monjeu/2.png");
 perso[2] = IMG_Load("monjeu/3.png");
 perso[3] = IMG_Load("monjeu/4.png");
 perso[4] = IMG_Load("monjeu/5.png");
 perso[5] = IMG_Load("monjeu/6.png");
 perso[6] = IMG_Load("monjeu/7.png");
 perso[7] = IMG_Load("monjeu/8.png");
 perso[8] = IMG_Load("monjeu/9.png");
 perso[9] = IMG_Load("monjeu/10.png");
 perso[10] = IMG_Load("monjeu/11.png");
 perso[11] = IMG_Load("monjeu/12.png");

/**/
if(testf==0)
{
paintflous();
}
else
{
paint();
}
/**/
SDL_EnableKeyRepeat(100, 100);
 while (continuer)
    {
coli ();
        SDL_PollEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
break;
 case SDL_KEYDOWN:
switch (event.key.keysym.sym)
            {
                case SDLK_RIGHT: /* Appui sur la touche entrer la variable entrer recoit 1 */
keyR=1;


		break;
                  case SDLK_UP:
keyUP=1;


                 
        }
break;

case SDL_KEYUP:
switch (event.key.keysym.sym)
            {
                case SDLK_RIGHT: /* Appui sur la touche entrer la variable entrer recoit 1 */
keyR=0;
test=0;


		break;
                  case SDLK_UP:
keyUP=0;

      break;
        }
break;

 }

if((ppx==flousx-50) && (ppy<=flousy+83)) 
{
testf=1;
}

if(keyR==1 )
{

coli();

test=1;
if (indice<10)
{
indice++;
}
else
{
indice=0;
  }

fondx-=15;
flousx-=15;


if(testf==0)
{
paintflous();
}
else
{
paint();
}

}




if(keyR==0 )
{

coli();

if(testf==0)
{
paintstopflous();
}
else
{
paintstop();
}

}





if(keyUP==1 )
{

coli();


if (indice<10)
{
indice++;
}
else
{
indice=0;
  }
ppy-=80;
fondx-=10;
flousx-=10;


if(testf==0)
{
paintstopflous();
}
else
{
paintstop();
}

}


}



SDL_FreeSurface(perso[11]);
SDL_FreeSurface(back);


 return EXIT_SUCCESS;
}












void sawer (SDL_Surface* img,int x ,int y ,SDL_Surface* ecr)
{
SDL_Rect pos;
pos.x=x;
pos.y=y;
SDL_BlitSurface(img,NULL,ecr,&pos);
}
void getPixelColor(int x, int y, SDL_Surface *map,Uint8 *r,Uint8 *g,Uint8 *b) /*Collision .. */
{
    if(x>0 && y>0 && x<map->w && y<map->h)
    {
        int bpp = map->format->BytesPerPixel;
      
        Uint8 *p = (Uint8 *)map->pixels + y * map->pitch + x * bpp;
        SDL_GetRGB(*(Uint32 *)p,map->format, r, g, b);
    }
    else // sinon on renvoie noir, pour éviter de planter dans certains cas
    {
        *r =0;
        *g =0;
        *b =0;
    } 
}



void paint()
{
//affichge hbout
SDL_Delay(50);
SDL_FillRect(ecran, NULL, SDL_MapRGB(back1->format, 0, 0, 0));
SDL_FillRect(ecran, NULL, SDL_MapRGB(back->format, 0, 0, 0));
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));

sawer(back1, fondx, fondy, ecran);
sawer(back, fondx, fondy, ecran);

sawer(perso[indice], ppx, ppy, ecran);


SDL_Flip(ecran);
//end affichage

}

void paintflous()
{
//affichge hbout
SDL_Delay(50);
SDL_FillRect(ecran, NULL, SDL_MapRGB(back1->format, 0, 0, 0));
SDL_FillRect(ecran, NULL, SDL_MapRGB(back->format, 0, 0, 0));
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));

sawer(back1, fondx, fondy, ecran);
sawer(back, fondx, fondy, ecran);
if(i<5)
{
i++;
}
else
{
i=0;
}
sawer(flous[i], flousx, flousy, ecran);
sawer(perso[indice], ppx, ppy, ecran);


SDL_Flip(ecran);
//end affichage

}

void paintstop()
{
//affichge hbout
SDL_Delay(50);
SDL_FillRect(ecran, NULL, SDL_MapRGB(back1->format, 0, 0, 0));
SDL_FillRect(ecran, NULL, SDL_MapRGB(back->format, 0, 0, 0));
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));

sawer(back1, fondx, fondy, ecran);

sawer(back, fondx, fondy, ecran);


sawer(perso[0], ppx, ppy, ecran);

SDL_Flip(ecran);
//end affichage

}

void paintstopflous()
{
//affichge hbout
SDL_Delay(50);
SDL_FillRect(ecran, NULL, SDL_MapRGB(back1->format, 0, 0, 0));
SDL_FillRect(ecran, NULL, SDL_MapRGB(back->format, 0, 0, 0));
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));


sawer(back1, fondx, fondy, ecran);
sawer(back, fondx, fondy, ecran);
if(i<5)
{
i++;
}
else
{
i=0;
}
sawer(flous[i], flousx, flousy, ecran);

sawer(perso[0], ppx, ppy, ecran);

SDL_Flip(ecran);
//end affichage

}



void coli ()
{
printf("\n%d ",r1);
r=0;
g=0;
b=0;

getPixelColor(ppx+perso[2]->w/2,ppy+perso[2]->h, ecran,&r1,&g1,&b1);

//getPixelColor(ppx,ppy+perso[2]->h, back1,&r2,&g2,&b2);

if((r1==r && g1==g && b1==b ) /*|| (r2==r && g2==g && b2==b)*/ )
   {

}

else
{
ppy+=10;
fondx-=10;
flousx-=10;

if(testf==0)
{

paintstopflous();
}
else
{
paintstop();
}
}

}
